import {
  getPage
} from '../../utils/config';
import wxApi from '../../utils/wxApi';
import api from '../../utils/api';
import {
  http,
  $loading,
  $toast
} from '../../utils/utils';
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    getPage: getPage,
    loading: 1,
    shopInfo: [],
    shopPage: 1,
    shopdone: false,
    shopabnor: false,
    loadmore: false,
    right: [{
      text: '删除',
    }, {
      text: '删除',
      style: 'color: #fa3e3e',
    }]
  },
  /**
   * 跳转店铺
   */
  visitStore(e) {
    const {
      id,
      index,
      shopId,
      open
    } = e.currentTarget.dataset || e.target.dataset;
    if (open) {
      this.toggleSwipe(e, false);
    } else {
      wxApi.navigateTo(getPage.adddesign, {
        id: id
      });
    }
  },

  /**
   * 开启
   */
  onOpen(e) {
    this.toggleSwipe(e, true);
  },
  fabu() {
    wxApi.navigateTo(getPage.adddesign);
  },
  toggleSwipe(e, t) {
    const data = e.currentTarget.dataset || e.target.dataset;
    console.log(data);
    const {
      goodsInfo,
      shopInfo
    } = this.data;

    if (data.goodsId !== undefined) {
      goodsInfo[data.index].isOpen = t;
      this.setData({
        goodsInfo
      });
    } else if (data.id !== undefined) {
      shopInfo[data.index].isOpen = t;
      this.setData({
        shopInfo
      });
    }
  },

  /**
   * 设计作品列表
   */
  getShopCollectionInfo(callback, a = false) {
    http.post(api.default, {
      data: {
        action: 'getdeignlist',
        shopid: this.data.shopid,
        pageindex: this.data.shopPage,
      }
    }, a).then((res) => {
      const data = res.data.data;
      const shopInfo = data.list;
      const pages = data.pages;

      /**
       * 增加判断是否打开
       */
      data.list.forEach((item) => {
        item.isOpen = false
      })

      //记录数据
      this.setData({
        shopPages: pages,
        shopabnor: pages == 0 ? true : false,
      });

      callback && callback(shopInfo);

      this.setData({
        shopdone: pages == 1 ? true : false,
      })
    });
  },

  /**
   * 删除设计作品
   */
  handleDelete(e) {
    const that = this;
    const data = e.currentTarget.dataset || e.target.dataset;
    const {
      tabsActive
    } = that.data;
    wx.showModal({
      cancelText: '取消',
      confirmText: '确定',
      content: '是否删除？',
      success(res) {
        if (res.confirm) {
          const shopInfo = that.data.shopInfo;
          http.post(api.default, {
            data: {
              action: 'del_design',
              shopid: that.data.shopid,
              id: data.id,
            }
          }).then((res) => {
            shopInfo.forEach((item, index) => {
              if (item.id == data.id) {
                shopInfo.splice(index, 1);
              }
            });
            $toast(`删除成功`, {
              icon: 'success'
            });
            that.setData({
              shopInfo: shopInfo,
              shopabnor: shopInfo.length == 0 ? true : false,
              shopdone: shopInfo.length == 0 ? false : true,
            })
          })

        }
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    //判断是否登录
    app.getLogin().then((res) => {
      this.setData({
        logged: res.code,
        userId: res.data.user.user_id,
        shopid: res.data.shop.ShopId,
        userRank: res.data.user.user_rank,
      });
      //获取店铺店铺
      this.getShopCollectionInfo((res) => {
        this.setData({
          shopInfo: res
        })
      });
    }).catch((res) => {
      this.setData({
        logged: res
      });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

    const pages = this.data.shopPages;
    let page = this.data.shopPage;
    page++;
    if (pages >= page) {
      this.setData({
        shopPage: page,
        loadmore: true,
      });

      //拉取数据
      this.getShopCollectionInfo((res) => {
        const shopInfo = this.data.shopInfo;
        this.setData({
          shopInfo: shopInfo.concat(res),
          loadmore: false,
          shopdone: pages == page ? true : false,
        });
      }, false);
    }

  },
})